<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        #data {
            width: 280px;
            border: 1px solid #000000;
            margin: 20px auto;
        }

        #data>p {
            display: flex;
        }

        #data>h5 {
            text-align: center;
        }

        #data>p>span {
            padding: 0 10px;
        }

        #prev,
        #next {
            cursor: pointer;
        }

        #nian {
            flex: 1;
            text-align: center;
        }

        #title {
            overflow: hidden;
            list-style: none;
            background: #ccc;
        }

        #title>li {
            float: left;
            width: 40px;
            height: 26px;
            line-height: 26px;
            text-align: center;
        }

        #date {
            overflow: hidden;
            list-style: none;
        }

        #date>li {
            float: left;
            width: 34px;
            height: 34px;
            margin: 1px 1px;
            border: 2px solid rgba(0, 0, 0, 0);
            line-height: 34px;
            text-align: center;
            cursor: pointer;
        }

        #date>.hover:hover {
            border: 2px solid red;
        }

        .active {
            color: red;
        }
    </style>
</head>

<body>
    <div id="data">
        <p>
            <span id="prev">上一月</span>
<!--            <span id="nian">2020</span>-->
          <select name="nian" id="nian">
                <option>请选择年份</option>
                <option value="2000">2000</option>
                <option value="2001">2001</option>
                <option value="2002">2002</option>
                <option value="2003">2003</option>
            </select>
            <span id="next">下一月</span>
        </p>
        <h5 id="yue">五月</h5>
        <ul id="title">
            <li>日</li>
            <li>一</li>
            <li>二</li>
            <li>三</li>
            <li>四</li>
            <li>五</li>
            <li>六</li>
        </ul>
        <ul id="date">
        </ul>
    </div>

</body>
<script>
    let date = new Date();//获取默认的时间对象
    let moth = date.getMonth();
    console.log(moth)
    add();//渲染页面
    function add() {
        let cyear = date.getFullYear();//获取当前的年份
        let cmonth = date.getMonth();//获取当前月份0-11
        let tday = date.getDate();//获取当前日期
        let montharr = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
        var nian = document.getElementById("nian");
        // document.getElementById("nian").innerHTML = cyear;
        nian.innerHTML=cyear;

        // nian.onchange = function () {
        //     console.log(nian.options[nian.selectedIndex].value);
        // }
        document.getElementById("yue").innerHTML = montharr[cmonth];
        //渲染日期
        let html = "";
        //获取每个月的第一天
        let fristday = new Date(cyear, cmonth, 1).getDay();
        for (var j = 0; j < fristday; j++) {
            html += "<li></li>";
        }
        let lastday = new Date(cyear, cmonth + 1, -0).getDate();//每个月天数
        for (var i = 1; i <= lastday; i++) {
            if (tday == i) {
                if (moth == cmonth) {
                    html += "<li class='active'>" + i + "</li>";
                } else {
                    html += "<li class='hover'>" + i + "</li>";
                }
            } else {
                html += "<li class='hover'>" + i + "</li>";
            }
        }
        document.getElementById("date").innerHTML = html;
    }
    document.getElementById('prev').onclick = function () {
        date.setMonth(date.getMonth() - 1);
        add();
    }
    document.getElementById('next').onclick = function () {
        date.setMonth(date.getMonth() + 1);
        add();
    }
</script>

</html>